package com.example.demo.service.impl;

import com.example.demo.entity.dto.ImageAvgNumDTO;
import com.example.demo.mapper.ImageMapper;
import com.example.demo.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author baisongyuan
 * @className ImageServiceImpl
 * @description
 * @date 2022/9/20 21:33
 */
@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private ImageMapper imgMapper;

    @Override
    public Map<Long, Integer> getImageAvgNumByTime(Long startTime, Long endTime) {
        List<ImageAvgNumDTO> dtos = imgMapper.getImageAvgNumByTime(startTime, endTime);
        Map<Long, Integer> imgNumMap = dtos.stream().collect(Collectors.toMap(ImageAvgNumDTO::getImgDate, ImageAvgNumDTO::getNum));
        Map<Long,Integer> res = new HashMap<>();
        //按照时间排序9.1 , 9.2 , 9.3
        List<Long> list = imgNumMap.keySet().stream().sorted().collect(Collectors.toList());
        for (int i = 0; i < list.size(); i++) {
            long key = list.get(i);
            int num = imgNumMap.get(key);
            if (i != 0){
                num+=res.get(list.get(i-1));
                num/=(i+1);
            }
            res.put(key,num);
        }
        return res;
    }
}
